package com.cyl.wechat;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cyl.h5.service.H5MemberService;
import com.cyl.wechat.response.JssdkConfigResponse;
import com.cyl.wechat.response.WechatUserAuth;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/no-auth/wechat")
public class WechatController {

    @Autowired
    private H5MemberService service;

    @Autowired
    private  WechatAuthService wechatAuthService;
    @GetMapping("/jssdk")
    public ResponseEntity<JssdkConfigResponse> getJssdkConfig(String url){
    	//url = "http://37099yt3zo89.vicp.fun/";
        String noncestr = WechatPayUtil.generateNonceStr();
        long timestamp = WechatPayUtil.getCurrentTimestamp();
        String accessToken = wechatAuthService.getAccessH5Token(); 
        String ticket = wechatAuthService.getTicket(accessToken);
        //System.err.println("token:"+accessToken);
        //System.err.println("ticket:"+ticket);
        JssdkConfigResponse response = wechatAuthService.getSign(accessToken,ticket, noncestr, timestamp, url);
        return ResponseEntity.ok(response);
    }
    
    
 
  
    @ApiOperation("获取微信用户授权信息")
    @PostMapping("/getWechatUserAuth")
    public ResponseEntity<WechatUserAuth> getWechatUserAuth(@RequestBody String data){
        return ResponseEntity.ok(service.getWechatUserAuth(data));
    }
}
